package googleAPI;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map.Entry;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonStreamParser;

public class GoogleConnect
{
	private JsonObject jObject;
	private URLConnection urlConnection;
	private InputStream inputStream;
	private InputStreamReader inputStreamReader;
	private BufferedReader bufferedReader;
	
	public JsonObject getDirections(String[] locations)
	{
		try
		{
			this.urlConnection = new URL("http://maps.googleapis.com/maps/api/directions/json?origin="+locations[0]+"&destination="+locations[1]+"&sensor=false&mode=driving").openConnection();
			this.inputStream = this.urlConnection.getInputStream();
			this.inputStreamReader = new InputStreamReader(this.inputStream);
			this.bufferedReader = new BufferedReader(this.inputStreamReader);
			JsonParser jsonParser = new JsonParser();
			JsonElement jsonElement = jsonParser.parse(this.bufferedReader);
			this.close();
			return jsonElement.getAsJsonObject();
		} catch (MalformedURLException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean close()
	{
		try
		{
			this.inputStream.close();
			this.inputStreamReader.close();
			this.bufferedReader.close();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		
		return false;
	}
	
	public void setRouteData(JsonObject jsonObject){
		
	}
	
	public void convertToRouteData(JsonObject jObject, String tabs, RouteData routeData, Point point, String lastKey) {
		for (Entry<String, JsonElement> map : jObject.entrySet()) {

			if (lastKey.equals("legs")){

				if (map.getKey().equals("distance")){
					JsonObject element = map.getValue().getAsJsonObject();
					routeData.setDistance(element.get("text").getAsString());
				}
				else if(map.getKey().equals("duration")) {

					JsonObject element = map.getValue().getAsJsonObject();
					routeData.setDuration(element.get("text").getAsString());					
				}
				else if(map.getKey().equals("end_address")){
					routeData.setEndAdress(map.getValue().toString());
				}
				else if(map.getKey().equals("end_location")){

					JsonObject latitude = map.getValue().getAsJsonObject();
					routeData.setEndLocationLatitude(latitude.get("lat").getAsString());

					JsonObject longitude = map.getValue().getAsJsonObject();
					routeData.setEndLocationLongitude(longitude.get("lng").getAsString());
				}
				else if(map.getKey().equals("start_address")){
					routeData.setStartAdress((map.getValue().toString()));

				}
				else if(map.getKey().equals("start_location")){

					JsonObject latitude = map.getValue().getAsJsonObject();
					routeData.setStartLocationLatitude(latitude.get("lat").getAsString());

					JsonObject longitude = map.getValue().getAsJsonObject();
					routeData.setStartLocationLongitude(longitude.get("lng").getAsString());
				}
			}
			else if(lastKey.equals("steps")){
				if (map.getKey().equals("distance")){
					point.setDistance(map.getValue().toString());
				}
				else if(map.getKey().equals("duration")){
					point.setDuration(map.getValue().toString());
				}
				else if(map.getKey().equals("end_location")){
					point.setEndLocationLatitude(map.getValue().toString());
					point.setEndLocationLatitude(map.getValue().toString());
				}
				else if(map.getKey().equals("start_location")){
					point.setStartLocationLatitude(map.getValue().toString());
					point.setStartLocationLatitude(map.getValue().toString());
				}
				else if(map.getKey().equals("travel_mode ")){
					routeData.addPoint(point);
					point = new Point();
				}
			}
		
			
			if (map.getValue().isJsonArray()) {
				JsonArray jArray = map.getValue().getAsJsonArray();
				for (JsonElement JsonElement : jArray) {
					convertToRouteData(JsonElement.getAsJsonObject(), (tabs+"\t"), routeData, point, map.getKey());
				}
			}
		}
	}
}
